﻿using HandyControl.Controls;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BaseUi.Views.PlcView
{
    /// <summary>
    /// WarmingbackBoxTakeChangeView.xaml 的交互逻辑
    /// </summary>
    public partial class WarmingbackBoxTakeChangeView : UserControlBase
    {
        private int Maxnumberc = 0;
        private int Maxnumberl = 0;
        private int numberc = 0;
        private int numberl = 0;
        private string numby = "";
        private string numbz = "";
        private string numbu = "";
        public WarmingbackBoxTakeChangeView()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 获取抬升高度
        /// </summary>
        private void getnumb()
        {
            var ioi = LocalStatic.Plc.Read<float>("1700");
            if (ioi.IsSucceed == false)
            {
                WarBoxch1g1.Text = "未接收";
            }
            else
            {
                WarBoxch1g1.Text = ioi.Value.ToString();
            }
            ioi = LocalStatic.Plc.Read<float>("1206");
            if (ioi.IsSucceed == false)
            {
                WarBoxch1g2.Text = "未接收";
            }
            else
            {
                WarBoxch1g2.Text = ioi.Value.ToString();
            }
            
        }
        /// <summary>
        /// 获取YZU1轴的当前位置
        /// </summary>
        private void getnowbnumber()
        {
            var ioi = LocalStatic.Plc.Read<float>("12");
            if (ioi.IsSucceed == false)
            {
                MacChBox2g1z1.Text = "未接收";
            }
            else
            {
                MacChBox2g1z1.Text = ioi.Value.ToString();
            }
            ioi = LocalStatic.Plc.Read<float>("14");
            if (ioi.IsSucceed == false)
            {
                MacChBox2g2z1.Text = "未接收";
            }
            else
            {
                MacChBox2g2z1.Text = ioi.Value.ToString();
            }
            ioi = LocalStatic.Plc.Read<float>("16");
            if (ioi.IsSucceed == false)
            {
                MacChBox2g3z1.Text = "未接收";
            }
            else
            {
                MacChBox2g3z1.Text = ioi.Value.ToString();
            }

        }
        /// <summary>
        /// 获取当前选择的层数和列数并计算出取放位的plc通信并获取取放位的值
        /// </summary>
            private void getchegb()
        {
            var ioi1 = LocalStatic.Plc.Read<Int16>("7");
            if (ioi1.IsSucceed == false)
            {
                MacChBox2z1.Text = "未接收";
                numberc = 0;
            }
            else
            {
                MacChBox2z1.Text = ioi1.Value.ToString();
                numberc = ioi1.Value;
            }
            ioi1 = LocalStatic.Plc.Read<Int16>("8");
            if (ioi1.IsSucceed == false)
            {
                MacChBox2z2.Text = "未接收"; 
                numberl = 0;
            }
            else
            {
                MacChBox2z2.Text = ioi1.Value.ToString();
                numberl = ioi1.Value;
            }
            if (numberc < 1 || numberc > Maxnumberc || numberl <1 || numberl > Maxnumberl)
            {
                Growl.Error("层列不正确，层" + numberc + "列" + numberl+"最大层"+ Maxnumberc + "最大列"+ Maxnumberl);
                MacChBox2g1z1.Text = "范围外";
                MacChBox2g2z1.Text = "范围外";
                MacChBox2g3z1.Text = "范围外";
                return;
                //numberc = 1;
                //numberl = 1;
                //MacChBox2z1.Text = numberc.ToString();
                //MacChBox2z2.Text = numberl.ToString();
            }
            else
            {
                int yget = ((numberc - 1) * (Maxnumberl*2)) + ((numberl - 1) * 2) + 2200;
                numby = yget.ToString();
                int zget = ((numberc - 1) * (Maxnumberl * 2)) + ((numberl - 1) * 2) + 2600;
                numbz = zget.ToString();
                int uget = ((numberc - 1) * (Maxnumberl * 2)) + ((numberl - 1) * 2) + 3000;
                numbu = uget.ToString();
                var ioi = LocalStatic.Plc.Read<float>(numby);
                if (ioi.IsSucceed == false)
                {
                    MacChBox2g1z1.Text = "未接收";
                }
                else
                {
                    MacChBox2g1z1.Text = ioi.Value.ToString();
                }
                ioi = LocalStatic.Plc.Read<float>(numbz);
                if (ioi.IsSucceed == false)
                {
                    MacChBox2g2z1.Text = "未接收";
                }
                else
                {
                    MacChBox2g2z1.Text = ioi.Value.ToString();
                }
                ioi = LocalStatic.Plc.Read<float>(numbu);
                if (ioi.IsSucceed == false)
                {
                    MacChBox2g3z1.Text = "未接收";
                }
                else
                {
                    MacChBox2g3z1.Text = ioi.Value.ToString();
                }
            }
        }

        private void warboxchButton_Click(object sender, RoutedEventArgs e)
        {
            getnewnumb();
            getnumb();
            getchegb();
        }

        /// <summary>
        /// 获取最大层数和列数
        /// </summary>
        private void getnewnumb()
        {
            var ioi1 = LocalStatic.Plc.Read<Int16>("1082");
            if (ioi1.IsSucceed == false)
            {
                Growl.Error("接收失败");
                Maxnumberc = 1;
            }
            else
            {
                Maxnumberc = ioi1.Value;
            }
            ioi1 = LocalStatic.Plc.Read<Int16>("1083");
            if (ioi1.IsSucceed == false)
            {
                Growl.Error("接收失败");
                Maxnumberl = 1;
            }
            else
            {
                Maxnumberl = ioi1.Value;
            }
        }

        private void Label_WarBoxDoubleClickch1g1(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(WarBoxch1g1.Text);
                var ioi = LocalStatic.Plc.Write<float>("1700", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void Label_WarBoxDoubleClickch1g2(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(WarBoxch1g2.Text);
                var ioi = LocalStatic.Plc.Write<float>("1206", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void loadFirst(object sender, EventArgs e)
        {
            getnewnumb();
            getnumb();
            getchegb();
        }
        /// <summary>
        /// 示数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void shishuButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                
                try
                {
                    float fait = float.Parse(MacChBox2g1z1.Text);
                    var ioi = LocalStatic.Plc.Write<float>(numby, fait);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                        return;
                    }
                    float fait2 = float.Parse(MacChBox2g2z1.Text);
                    var ioi2 = LocalStatic.Plc.Write<float>(numbz, fait2);
                    if (ioi2.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                        return;
                    }
                    float fait3 = float.Parse(MacChBox2g3z1.Text);
                    var ioi3 = LocalStatic.Plc.Write<float>(numbu, fait3);
                    if (ioi3.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                        return;
                    }
                    getnewnumb();
                    getchegb();
                    if (numberc < 1 || numberc > Maxnumberc || numberl < 1 || numberl > Maxnumberl)
                    {
                        Growl.Error("层列不正确，层" + numberc + "列" + numberl + "最大层" + Maxnumberc + "最大列" + Maxnumberl);
                        MacChBox2g1z1.Text = "范围外";
                        MacChBox2g2z1.Text = "范围外";
                        MacChBox2g3z1.Text = "范围外";
                        return;
                        //numberc = 1;
                        //numberl = 1;
                        //MacChBox2z1.Text = numberc.ToString();
                        //MacChBox2z2.Text = numberl.ToString();
                    }
                    getnowbnumber();
                }
                catch (Exception ee)
                {

                    Growl.Error(ee.ToString());
                }
                //getchegb();
                //if (numberc < 1 || numberc > 7 || numberl < 0 || numberl > 7)
                //{
                //    Growl.Error("层列不正确，层" + numberc + "列" + numberl);
                //}
                //else
                //{
                //    int yget = ((numberc - 1) * 7) + ((numberl - 1) * 2) + 2200;
                //    numby = yget.ToString();
                //    int zget = ((numberc - 1) * 7) + ((numberl - 1) * 2) + 2600;
                //    numbz = zget.ToString();
                //    int uget = ((numberc - 1) * 7) + ((numberl - 1) * 2) + 3000;
                //    numbu = uget.ToString();
                //    var ioi = LocalStatic.Plc.Read<float>(numby);
                //    if (ioi.IsSucceed == false)
                //    {
                //        MacChBox2g1z1.Text = "未接收";
                //    }
                //    else
                //    {
                //        MacChBox2g1z1.Text = ioi.Value.ToString();
                //    }
                //    ioi = LocalStatic.Plc.Read<float>(numbz);
                //    if (ioi.IsSucceed == false)
                //    {
                //        MacChBox2g2z1.Text = "未接收";
                //    }
                //    else
                //    {
                //        MacChBox2g2z1.Text = ioi.Value.ToString();
                //    }
                //    ioi = LocalStatic.Plc.Read<float>(numbu);
                //    if (ioi.IsSucceed == false)
                //    {
                //        MacChBox2g3z1.Text = "未接收";
                //    }
                //    else
                //    {
                //        MacChBox2g3z1.Text = ioi.Value.ToString();
                //    }
                //}
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }
        /// <summary>
        /// 更新层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_Changc(object sender, MouseButtonEventArgs e)
        {
            try
            {
                short ser = Convert.ToInt16(MacChBox2z1.Text);
                if (ser > Maxnumberc || ser < 1)
                {

                    Growl.Error("数值大于"+ Maxnumberc + "或小于1");
                    return;
                }
                var ioi = LocalStatic.Plc.Write<Int16>("7", ser);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }
        /// <summary>
        /// 更新层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox_KeyDowncheng(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    short ser = Convert.ToInt16(MacChBox2z1.Text);
                    if (ser > Maxnumberc || ser < 1)
                    {

                        Growl.Error("数值大于"+ Maxnumberc + "或小于1");
                        return;
                    }
                    var ioi = LocalStatic.Plc.Write<Int16>("7", ser);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                }
                catch (Exception ee)
                {
                    Growl.Error(ee.ToString());
                }
            }
        }
        /// <summary>
        /// 更新列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_Changl(object sender, MouseButtonEventArgs e)
        {
            try
            {
                short ser = Convert.ToInt16(MacChBox2z2.Text);
                if (ser > Maxnumberl || ser < 1)
                {

                    Growl.Error("数值大于"+ Maxnumberl+"或小于1");
                    return;
                }
                var ioi = LocalStatic.Plc.Write<Int16>("8", ser);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }
        /// <summary>
        /// 更新列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox_KeyDownlie(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    short ser = Convert.ToInt16(MacChBox2z2.Text);
                    if (ser >Maxnumberl || ser < 1)
                    {

                        Growl.Error("数值大于"+ Maxnumberl+"或小于1");
                        return;
                    }
                    var ioi = LocalStatic.Plc.Write<Int16>("8", ser);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                }
                catch (Exception ee)
                {
                    Growl.Error(ee.ToString());
                }
            }
        }
        /// <summary>
        /// 上传Y轴
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_Changy(object sender, MouseButtonEventArgs e)
        {
            if (!MacChBox2z1.Text.Equals(numberc.ToString()))
            {
                Growl.Error("当前行数值有问题，可能未上传，请点击示数");
                return;
            }
            if (!MacChBox2z2.Text.Equals(numberl.ToString()))
            {
                Growl.Error("当前列数值有问题，可能未上传，请点击示数");
                return;
            }
            if (numberc < 1 || numberc > Maxnumberc || numberl < 1 || numberl > Maxnumberl)
            {
                Growl.Error("层列不正确，层" + numberc + "列" + numberl + "最大层" + Maxnumberc + "最大列" + Maxnumberl);
                return;
            }
            if (numby == "")
            {
                Growl.Error("plc无端口，可能未点击示数");
                return;
            }
            try
            {
                float fait = float.Parse(MacChBox2g1z1.Text);
                var ioi = LocalStatic.Plc.Write<float>(numby, fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }
        /// <summary>
        /// 上传Z轴
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_Changz(object sender, MouseButtonEventArgs e)
        {
            if (!MacChBox2z1.Text.Equals(numberc.ToString()))
            {
                Growl.Error("当前行数值有问题，可能未上传，请点击示数");
                return;
            }
            if (!MacChBox2z2.Text.Equals(numberl.ToString()))
            {
                Growl.Error("当前列数值有问题，可能未上传，请点击示数");
                return;
            }
            if (numberc < 1 || numberc > Maxnumberc || numberl < 1 || numberl > Maxnumberl)
            {
                Growl.Error("层列不正确，层" + numberc + "列" + numberl + "最大层" + Maxnumberc + "最大列" + Maxnumberl);
                return;
            }
            if (numbz == "")
            {
                Growl.Error("plc无端口，可能未点击示数");
                return;
            }
            try
            {
                float fait = float.Parse(MacChBox2g2z1.Text);
                var ioi = LocalStatic.Plc.Write<float>(numbz, fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }
        /// <summary>
        /// 上传U1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_Changu(object sender, MouseButtonEventArgs e)
        {
            if (!MacChBox2z1.Text.Equals(numberc.ToString()))
            {
                Growl.Error("当前行数值有问题，可能未上传，请点击示数");
                return;
            }
            if (!MacChBox2z2.Text.Equals(numberl.ToString()))
            {
                Growl.Error("当前列数值有问题，可能未上传，请点击示数");
                return;
            }
            if (numberc < 1 || numberc > Maxnumberc || numberl < 1 || numberl > Maxnumberl)
            {
                Growl.Error("层列不正确，层" + numberc + "列" + numberl + "最大层" + Maxnumberc + "最大列" + Maxnumberl);
                return;
            }
            if (numbu=="")
            {
                Growl.Error("plc无端口，可能未点击示数");
                return;
            }
            try
            {
                float fait = float.Parse(MacChBox2g3z1.Text);
                var ioi = LocalStatic.Plc.Write<float>(numbu, fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }
    }
}
